﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Web;

namespace Kaixin.BLL.Helper
{
    /// <summary>
    /// 用于处理请求和接收页面的辅助类
    /// </summary>
    public abstract class WebHelper
    {
         
        private static CookieContainer myCookieContainer = new CookieContainer();
     
        private static string GetHtml(WebResponse response)
        {
            Stream receiveStream = response.GetResponseStream();
            StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
            string result = readStream.ReadToEnd();
            readStream.Close();
            response.Close();
            return result;
        }

        public static string Encode(string data)
        {
            return HttpUtility.UrlEncode(data);
        }

        private static HttpWebRequest GetHttpWebRequest(string url, string method, string postData)
        {
            return GetHttpWebRequest(url, method, postData, string.Empty);
        }
        private static HttpWebRequest GetHttpWebRequest(string url, string method, string postData, string referer)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            
            request.Method = method;
            request.Accept = "application/x-shockwave-flash, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            request.ServicePoint.Expect100Continue = false;
            request.KeepAlive = true;
            request.ProtocolVersion = HttpVersion.Version11;
            request.Timeout = 120000;
            if (referer!=string.Empty)
            {
                request.Referer = referer;
            }
            request.Headers.Add("Accept-Language", "zh-cn");
           // request.Headers.Add("Accept-Encoding", "gzip, deflate");
            request.Headers.Add("Pragma", "no-cache");
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = myCookieContainer;
            if (method.ToLower() == "post" && !string.IsNullOrEmpty(postData))
            {
                byte[] data = Encoding.GetEncoding("GB2312").GetBytes(postData);
                request.ContentLength = data.Length;
                Stream s = request.GetRequestStream();
                s.Write(data, 0, data.Length);
                s.Close();
            }

            return request;
        }
        
        public static string Post(string url, string postData)
        {
            return Post(url, postData, string.Empty);
        }
        public static string Post(string url, string postData, string referer)
        {
            Thread.Sleep(Config.CreateConfig().VisitInterval());
            return GetHtml(GetHttpWebRequest(url, "post", postData,referer).GetResponse());
        }

        public static string Get(string url)
        {
            Thread.Sleep(Config.CreateConfig().VisitInterval());
            return GetHtml(GetHttpWebRequest(url, "get", string.Empty).GetResponse());
        }

    }
}